﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ObjectManager;
using Process;

namespace QuanLyDiem
{
    public partial class frmBoMon_Sua : Form
    {
        private static frmBoMon_Sua frm = null;
        private BoMon _boMon = null;
        private BoMon _boMonOld = new BoMon();

        public event UpdateBoMon UpdateBoMonSuccess = null;

        private frmBoMon_Sua()
        {
            InitializeComponent();
        }

        private frmBoMon_Sua(BoMon boMon)
        {
            InitializeComponent();
            this._boMon = boMon;
            this._boMonOld.ThongTin.Ma = boMon.ThongTin.Ma;
            this._boMonOld.ThongTin.KyHieu = boMon.ThongTin.KyHieu;
            this._boMonOld.ThongTin.Ten = boMon.ThongTin.Ten;
        }

        public static frmBoMon_Sua Create()
        {
            if (frm == null)
            {
                frm = new frmBoMon_Sua();
            }
            frm.UpdateData();
            return frm;
        }

        public static frmBoMon_Sua Create(BoMon boMon)
        {
            if (frm == null)
            {
                frm = new frmBoMon_Sua(boMon);
            }
            frm._boMon = boMon;
            frm._boMonOld.ThongTin.Ma = boMon.ThongTin.Ma;
            frm._boMonOld.ThongTin.KyHieu = boMon.ThongTin.KyHieu;
            frm._boMonOld.ThongTin.Ten = boMon.ThongTin.Ten;
            frm.UpdateData();
            return frm;
        }

        private void UpdateData()
        {
            txtMa.Text = this._boMon.ThongTin.Ma.ToString();
            txtKyHieu.Text = this._boMon.ThongTin.KyHieu;
            txtTen.Text = this._boMon.ThongTin.Ten;
        }

        private void frmBoMon_Sua_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (frm != null)
            {
                frm.Dispose();
                frm = null;
            }
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            txtKyHieu.Text = txtKyHieu.Text.Trim();
            txtTen.Text = xString.Name(txtTen.Text);
            if (string.IsNullOrEmpty(txtTen.Text))
            {
                MessageBox.Show("Phải nhập tên", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtTen.Focus();
                return;
            }
            _boMon.ThongTin.Ma = int.Parse(txtMa.Text);
            _boMon.ThongTin.Ten = txtTen.Text;
            _boMon.ThongTin.KyHieu = txtKyHieu.Text;
            if (_boMon.ThongTin.Ten == _boMonOld.ThongTin.Ten && _boMon.ThongTin.KyHieu == _boMonOld.ThongTin.KyHieu)
            {
                this.Close();
            }
            else
            {
                if (_boMon.Update() > 0)
                {
                    InfoUpdateBoMon info = new InfoUpdateBoMon();
                    info.BoMon = this._boMon;
                    UpdateBoMonSuccess(this, info);
                    this.Close();
                }
                else
                {
                    MessageBox.Show("Có vấn đề khi sửa", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
        }

        private void txtKyHieu_Leave(object sender, EventArgs e)
        {
            txtKyHieu.Text = txtKyHieu.Text.Trim();
        }

        private void txtTen_Leave(object sender, EventArgs e)
        {
            txtTen.Text = xString.Name(txtTen.Text);
        }
    }
}
